home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ARITCLIP.ZIP / ARTHCLIP.TXT next >
Encoding:
Text File  |  1996-09-18  |  11.1 KB  |  287 lines

  1.                   -----===== POLYGON CLIPPING ====----
  2.  
  3.                                 by:      Adrian Brown
  4.                                 version: 1.0
  5.                                 date:    17th September 1996
  6.  
  7. ******************************************************************************
  8.   This paper is designed to take you through the step of 2 polygon clipping
  9. techniques.  The first is the frequently used scanline by scanline technique
  10. while the second is arithmetic clipping.  This involves clipping the polygon
  11. before you actuall go to draw it, Adding new vertices where required to
  12. produce a new polygon which has coordinates that are all on the screen.
  13. ******************************************************************************
  14. ** DISCLAIMER
  15.  
  16. I assume no responsibility whatsoever for any effect that this file, the
  17. information contained therein or the use thereof has.  No warranty is
  18. provided nor implied with this information.
  19.  
  20. ******************************************************************************
  21. ** Contents
  22.  
  23. 1)  What is clipping?
  24.     a) 2D Sprite based clipping.
  25.     b) Edge scanning a polygon.
  26. 2)  Scanline by scanline clipping.
  27.     a) Clipping to the Top and Bottom.
  28.     b) Clipping to the Left and Right.
  29. 3)  Arithmetic clipping.
  30.  
  31. ******************************************************************************
  32. ** 2) What is clipping?
  33.  
  34. a) 2D Sprite based clipping.
  35. ----------------------------
  36.  
  37.   Clipping is a way of making sure that anything you draw to a fixed size
  38. screen stays within the boundaries of that screen.  In a simple 2d sprite
  39. based system a clipping routine would have looked something like this.
  40.  
  41. /* SAMPLE CODE FOR BASIC 2D SPRITE CLIP
  42.   this is only example code. I do not promise that it will compile or work.*/
  43.  
  44.     if (Sprite.X > SCREEN_WIDTH ▌▌ (Sprite.X + Sprite.W) < 0 ▌▌
  45.         Sprite.Y > SCREEN_HEIGHT ▌▌ (Sprite.Y + Sprite.H) < 0)
  46.     {
  47.         // Sprite is fully off screen.  No need to draw.
  48.         return;
  49.     }
  50.  
  51.     if (Sprite.X < 0)
  52.     {
  53.         // Sprite needs Clipping on left of screen
  54.         Sprite.GfxX += -Sprite.X;
  55.         Sprite.W += Sprite.X;
  56.         Sprite.X = 0;
  57.     }
  58.  
  59.     if ((Sprite.X + Sprite.W) > SCREEN_WIDTH)
  60.     {
  61.         // Sprite need Clipping on right of screen
  62.         Sprite.W -= (Sprite.X + Sprite.W) - SCREEN_WIDTH;
  63.     }
  64.  
  65.     if (Sprite.Y < 0)
  66.     {
  67.         // Sprite needs Clipping on top of screen
  68.         Sprite.GfxY += -Sprite.Y;
  69.         Sprite.H += Sprite.Y;
  70.         Sprite.Y = 0;
  71.     }
  72.  
  73.     if ((Sprite.Y + Sprite.H) > SCREEN_HEIGHT)
  74.     {
  75.         // Sprite need Clipping on bottom of screen
  76.         Sprite.H -= (Sprite.Y + Sprite.H) - SCREEN_HEIGHT;
  77.     }
  78.  
  79. * END OF EXAMPLE CODE *
  80.  
  81.   This first of all checks to see if the sprite is even on screen.  It then
  82. proceeds to check each side of the sprite with the sides of the screen.  If
  83. any edges need clipping in moves the appropriate X/W or W/H values.  It also
  84. moves the start graphic locations to match.
  85.  
  86.   The principles behind this simple 2d sprite based clipping are the same when
  87. it comes to clipping polygons.  You are still trying to fit an object into a
  88. screen size.  I am going to cover 2 methods I have used to clip polygons,
  89. both of them clip the 2d polygon.  This means if you are using it in a 3d
  90. based environment you must convert the polygon x,y,z coordinates into screen
  91. x,y coordinates before you can use these methods.  I also assume that you
  92. use an edge scanning polygon technique to draw your polygons.
  93.  
  94. b) Edge scanning a polygon.
  95. ---------------------------
  96.  
  97.   Edge scanning polygon draw routines use a method of tracing down each edge
  98. of a polygon.  The X coordinate of the line is stored in a table at the
  99. corresponding Y coordinate. e.g. if a point on the edge of a polygon was at
  100. X = 10, Y = 101 then at entry 101 in the edge table you would store the value
  101. 10.
  102.  
  103.   For more information about Edge scanning polygon draw routines please
  104. refer to the works of Chris Egerter in his tutorials 1,2 and 3.  These can
  105. be found at many ftp sites including x2ftp.oulu.fi/pub/msdos/programming/wgt.
  106.  
  107. ******************************************************************************
  108. ** 2) Scanline by Scanline clipping.
  109.  
  110. a) Clipping the top and bottom.
  111. -------------------------------
  112.  
  113.   Clipping the top and bottom of a polygon is done in the line draw routine.
  114. Before you start to draw your line you check to see if the Y start coordinate
  115. of the line is less than 0.  If it is then you must move the starting X
  116. coordinate to the point where Y = 0.  This is generally done using the
  117. gradient of the line.
  118.  
  119.                       X1, Y1
  120.                        /▌
  121.                      /  ▌
  122.                    /    ▌
  123.         ---------/------▌------------- line Y = 0
  124.                /        ▌
  125.              /          ▌           gradient of line X1,Y1 - X2,Y2 =
  126.            /            ▌                  (Y2 - Y1) / (X2 - X1)
  127.          /______________▌
  128.       X2, Y2
  129.  
  130.   Using the gradient of the line we can say where any particular coordinate
  131. is based upon the other.  In the case of clipping we want to know what the X
  132. will be when Y = 0.  Lets run this through an example:-
  133.  
  134.                       100, -50
  135.                        /▌
  136.                      /  ▌ d
  137.                    /    ▌ 
  138.         ---------/------▌------------- line Y = 0
  139.                /    c   ▌ b
  140.              /          ▌
  141.            /            ▌
  142.          /______________▌
  143.       0, 50     a
  144.  
  145. where:-
  146. a = X2 - X1 = -100
  147. b = Y2 - Y1 = 100
  148. c = Wanted value.
  149. d = portion of line above Y = 0 (-Y1) = 50
  150.  
  151.   The gradient for this line would be  b / a
  152.                                    = 100 / -100
  153.                                    = -1
  154.   Math's states that since the gradient of a triangle is the same no matter
  155. where you take the values from b/a will equal d/c.  We can use this
  156. information by rearranging it to give us the unknown value.
  157.  
  158.        b   d            b                                           d x a
  159.        - = -      ->    - x c = d    ->    b x c = d x a   ->   c = -----
  160.        a   c            a                                             b
  161.  
  162.   Plug in the values we know to the formula at the end and you get:-
  163.  
  164.       50 x -100
  165.       ---------   =   -50
  166.          100
  167.  
  168.   You simply add the value you get from this formula to X1 and you get the
  169. value for X1 where Y1 = 0.
  170.  
  171.   The same principles can be used to clip any lines which go off the bottom
  172. of the screen.
  173.  
  174. b) Clipping the left and right.
  175. -------------------------------
  176.  
  177.   Clipping to the left and right edges of the screen is very easy in scanline
  178. by scanline clipping.  When you read the values from your edge table you
  179. check to see if the starting value is off the left hand edge.  If it is you
  180. set the value to be the left hand edge.  If the finishing edge is off the
  181. right hand side of screen you can set the finishing edge to be the right hand
  182. side of screen.
  183.  
  184.   The inner loop for a draw routine using this technique would look something
  185. like this:-
  186.  
  187. /* SAMPLE CODE FOR AN INNER LOOP
  188.   this is only example code. I do not promise that it will compile or work.*/
  189.  
  190.   // Get the edges to draw between.                      
  191.   X1 = EdgeTable[Y][0];
  192.   X2 = EdgeTable[Y][1];
  193.  
  194.   // Check for clipping.
  195.   if (X1 < 0)
  196.   {
  197.     X1 = 0;
  198.   }
  199.  
  200.   if (X2 > SCREEN_WIDTH)
  201.   {
  202.     X2 = SCREEN_WIDTH
  203.   }
  204.  
  205.   for(;X1 < X2; X1++)
  206.   {
  207.     *(Dest + X1) = PixelValue;
  208.   }
  209.  
  210. * END OF EXAMPLE CODE *
  211.  
  212.   The next style of clipping is much harder to implement but I found that it
  213. is much faster, especially on larger polygons.
  214.  
  215. ******************************************************************************
  216. ** 3) Arithmetic Clipping
  217.  
  218.   Arithmetic clipping involves removing the parts of the polygon which are off
  219. the screen and adding vertices to create a new polygon.  This new polygon is
  220. the same shape as the old apart from there are no edges off the edges of the
  221. screen.
  222.  
  223.   The principles are very simple, but I have found that the clipping must be
  224. done in a certain order.  This is the Top then Bottom then Left and final
  225. Right hand edges.  If you do not follow this order some very strange shapes
  226. will appear in place of your polygon.
  227.  
  228.   Lets take a normal polygon:-
  229.  
  230.                               X1,Y1
  231.                                /\
  232.                              /    \
  233.            ________________/________\____________________
  234.            ▌       X4,Y4 /            \ X2,Y2           ▌
  235.            ▌             \            /                 ▌
  236.            ▌               \        /                   ▌
  237.            ▌                 \    /                     ▌
  238.            ▌                   \/                       ▌
  239.            ▌                 X3,Y3                      ▌
  240.  
  241.   Now to clip this arithmetically we would first clip the lines that are off
  242. the top of screen.  There are two which meet this description.  X1,Y1 - X4,Y4
  243. and X1,Y1 - X2,Y2.  When we clip these lines we will produce an extra vertex
  244. and an extra line.  This is because at the moment the two lines both share the
  245. vertex X1,Y1, but when they are clipped we will need vertices where the two
  246. lines cross the top of screen.  These two points will not be the same and so
  247. we have to insert a new vertex.
  248.  
  249.            _______________a__________b___________________
  250.            ▌       X4,Y4 /            \ X2,Y2           ▌
  251.            ▌             \            /                 ▌
  252.            ▌               \        /                   ▌
  253.            ▌                 \    /                     ▌
  254.            ▌                   \/                       ▌
  255.            ▌                 X3,Y3                      ▌
  256.  
  257.   The new polygon has 5 lines, not 4.
  258. line 1  = a - b
  259. line 2  = b - X2,Y2
  260. line 3  = X2,Y2 - X3,Y3
  261. line 4  = X3,Y3 - X4,Y4
  262. line 5  = X4,Y4 - a
  263.  
  264.   The coordinates of the new points can be calculated in the same way as we
  265. done for the top/bottom clipping above.  We go through all the lines checking
  266. and clipping to the top and bottom of screen and then we use exactly the same
  267. principles for clipping to the left and right edges of screen.  When you clip
  268. to L/R edges you must use the new points gained in the T/B clipping.
  269.  
  270. ******************************************************************************
  271. ** 4) Closing words
  272.  
  273.   These methods can be used when drawing texture mapped polygons with very
  274. little alteration.  The only thing that needs to be added to the clipping
  275. routines are a few lines to move the texture coordinates by the same amount
  276. as the screen coordinates.  Likewise with Gouraud shaded polygons.
  277.  
  278.   If you are feeling brave you can even use the same techniques to clip
  279. polygons to other planes an hence have one object be cut by another.  This
  280. does require clipping in 3d space but this is all very similar to the 2d
  281. clipping mentioned above.
  282.  
  283. ******************************************************************************
  284.   If you want to drop me a line an ask anything about the clipping routines
  285. above, or anything else for that matter, send an email to the above address.
  286. ******************************************************************************
  287.